import apiClient from './config'
import type { 
  User, 
  CreateUserRequest, 
  UpdateUserRequest, 
  UserListParams,
  ApiResponse,
  PaginatedResponse 
} from './types'

export const usersApi = {
  // 获取用户列表
  getUsers: (params?: UserListParams): Promise<ApiResponse<PaginatedResponse<User>>> => {
    return apiClient.get('/users', { params })
  },

  // 获取用户详情
  getUser: (id: number): Promise<ApiResponse<User>> => {
    return apiClient.get(`/users/${id}`)
  },

  // 创建用户
  createUser: (data: CreateUserRequest): Promise<ApiResponse<User>> => {
    return apiClient.post('/users', data)
  },

  // 更新用户
  updateUser: (data: UpdateUserRequest): Promise<ApiResponse<User>> => {
    return apiClient.put(`/users/${data.id}`, data)
  },

  // 删除用户
  deleteUser: (id: number): Promise<ApiResponse<any>> => {
    return apiClient.delete(`/users/${id}`)
  },

  // 批量删除用户
  batchDeleteUsers: (ids: number[]): Promise<ApiResponse<any>> => {
    return apiClient.delete('/users/batch', { data: { ids } })
  },

  // 更新用户状态
  updateUserStatus: (id: number, status: 'active' | 'inactive' | 'banned'): Promise<ApiResponse<any>> => {
    return apiClient.patch(`/users/${id}/status`, { status })
  },

  // 重置用户密码
  resetUserPassword: (id: number, newPassword: string): Promise<ApiResponse<any>> => {
    return apiClient.patch(`/users/${id}/password`, { password: newPassword })
  },

  // 获取用户角色
  getUserRoles: (id: number): Promise<ApiResponse<any[]>> => {
    return apiClient.get(`/users/${id}/roles`)
  },

  // 分配用户角色
  assignUserRoles: (id: number, roleIds: number[]): Promise<ApiResponse<any>> => {
    return apiClient.post(`/users/${id}/roles`, { roleIds })
  },

  // 导出用户数据
  exportUsers: (params?: UserListParams): Promise<Blob> => {
    return apiClient.get('/users/export', { 
      params,
      responseType: 'blob'
    })
  },

  // 导入用户数据
  importUsers: (file: File): Promise<ApiResponse<any>> => {
    const formData = new FormData()
    formData.append('file', file)
    return apiClient.post('/users/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}